package com.industrial.premu.controller.chart;


import cn.hutool.core.util.ArrayUtil;
import com.industrial.common.core.web.controller.BaseController;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.premu.domain.PreMuBenefitEvaluation;
import com.industrial.premu.dto.PreMuChartsDto;
import com.industrial.premu.service.IPreMuBenefitEvaluationService;
import com.industrial.premu.service.IPreMuBenfitEvaluationChatService;
import com.industrial.premu.util.PageUtils;
import com.industrial.premu.vo.PreMuChartsVo;
import com.industrial.system.api.model.LoginUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.*;

/**
 * 区域行业指标
 */
@RestController
@RequestMapping("/chart/areaIndustry")
public class PreMuAreaIndustryController extends BaseController {
    @Autowired
    private IPreMuBenefitEvaluationService preMuBenefitEvaluationService;

    @Autowired
    private IPreMuBenfitEvaluationChatService preMuBenfitEvaluationChatService;

    @GetMapping(value = "/areaIndustry")
    public AjaxResult areaIndustry(PreMuChartsDto preChartsDto) {
        List<PreMuChartsVo> chartsVoList= new ArrayList<>();
        int years=Integer.valueOf(preChartsDto.getYears());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Set<String> roles= loginUser.getRoles();
        if(roles.contains("x_01_011")||roles.contains("along_yellow_river")){
            String county=  loginUser.getSysUser().getDistrict();
            preChartsDto.setCounty(county);
        }else if(roles.contains("s_01_011")){
            String city=  loginUser.getSysUser().getCity();
            preChartsDto.setCity(city);
        }else if(roles.contains("p_01_011")){
            String province=  loginUser.getSysUser().getProvince();
            preChartsDto.setProvince(province);
        }else if(roles.contains("admin")){
            preChartsDto.setProvince("370000");
        }
        if(years>=2019){
            preChartsDto.setEndYears(String.valueOf(years));
            int  startYears=years-4;
            if(startYears>2019){
                preChartsDto.setStartYears(String.valueOf(startYears));
            }else{
                preChartsDto.setStartYears("2019");
            }
            preChartsDto.setYears("");
        }
        String  indexName=preChartsDto.getIndexName();
        switch (indexName) {
            case "单位用地税收": preChartsDto.setIndexName("dwydss");
                break;
            case "单位能耗销售收入":preChartsDto.setIndexName("dwnhxs");
                break;
            case "单位能耗营业收入":preChartsDto.setIndexName("dwnhxs");
                break;
            case "单位污染物排放销售收入":preChartsDto.setIndexName("dwwrwpf");
                break;
            case "单位污染物排放营业收入":preChartsDto.setIndexName("dwwrwpf");
                break;
            case "研发经费投入强度":preChartsDto.setIndexName("yfjftr");
                break;
            case "全员劳动生产率":preChartsDto.setIndexName("qyldscl");
                break;
            case "单位用地销售收入":preChartsDto.setIndexName("dwydxs");
                break;
            case "单位用地营业收入":preChartsDto.setIndexName("dwydxs");
                break;
            case "收入利润率" : preChartsDto.setIndexName("srlrl");
                break;
            default:
                logger.info("无此选项!");
        }
       // List<PreMuChartsVo> chartsVoList= preMuBenefitEvaluationService.groupAreaIndustry(preChartsDto);
        if(roles.contains("x_01_011")||roles.contains("along_yellow_river")){
            String county=  loginUser.getSysUser().getDistrict();
            preChartsDto.setCounty(county);
            List<PreMuChartsVo> pList=preMuBenfitEvaluationChatService.groupAreaIndustryProvince(preChartsDto);
            List<PreMuChartsVo> cList=preMuBenfitEvaluationChatService.groupAreaIndustryCityAndCounty(preChartsDto);
            for (int i = 0; i < pList.size(); i++) {
                PreMuChartsVo preMuChartsVo =new PreMuChartsVo();
                preMuChartsVo.setYears(pList.get(i).getYears());
                preMuChartsVo.setPIndexNum(pList.get(i).getPIndexNum());
                chartsVoList.add(preMuChartsVo);
            }
            for (int i = 0; i < cList.size(); i++) {
                for (int j = 0; j < chartsVoList.size(); j++) {
                if(cList.get(i).getYears().contains(chartsVoList.get(j).getYears())){
                    chartsVoList.get(j).setCIndexNum(cList.get(i).getCIndexNum());
                }
                }
            }
        }else if(roles.contains("s_01_011")){
            List<PreMuChartsVo> pList=preMuBenfitEvaluationChatService.groupAreaIndustryProvince(preChartsDto);
            List<PreMuChartsVo> sList=preMuBenfitEvaluationChatService.groupAreaIndustryCityAndCounty(preChartsDto);
            for (int i = 0; i < pList.size(); i++) {
                PreMuChartsVo preMuChartsVo =new PreMuChartsVo();
                preMuChartsVo.setYears(pList.get(i).getYears());
                preMuChartsVo.setPIndexNum(pList.get(i).getPIndexNum());
                chartsVoList.add(preMuChartsVo);
            }
            for (int i = 0; i < sList.size(); i++) {
                for (int j = 0; j < chartsVoList.size(); j++) {
                    if(sList.get(i).getYears().contains(chartsVoList.get(j).getYears())){
                        chartsVoList.get(j).setCIndexNum(sList.get(i).getCIndexNum());
                    }
                }
            }
        }else if(roles.contains("p_01_011")){
            chartsVoList=  preMuBenfitEvaluationChatService.groupAreaIndustryProvince(preChartsDto);
        }else if(roles.contains("admin")){
            preChartsDto.setProvince("370000");
            chartsVoList=  preMuBenfitEvaluationChatService.groupAreaIndustryProvince(preChartsDto);
        }

        return  success(chartsVoList);
    }

    @GetMapping(value = "/groupCompanyByIndustry")
    public AjaxResult groupCompanyByIndustry(PreMuChartsDto preChartsDto) {

        LoginUser loginUser = SecurityUtils.getLoginUser();
        Set<String> roles= loginUser.getRoles();
        if(roles.contains("x_01_011")||roles.contains("along_yellow_river")){
            String county=  loginUser.getSysUser().getDistrict();
            preChartsDto.setCounty(county);
        }else if(roles.contains("s_01_011")){
            String city=  loginUser.getSysUser().getCity();
            preChartsDto.setCity(city);
        }else if(roles.contains("p_01_011")){
            String province=  loginUser.getSysUser().getProvince();
            preChartsDto.setProvince(province);
        }
        String  indexName=preChartsDto.getIndexName();
        switch (indexName) {
            case "单位用地税收": preChartsDto.setIndexName("dwydss");
                break;
            case "单位能耗销售收入":preChartsDto.setIndexName("dwnhxs");
                break;
            case "单位能耗营业收入":preChartsDto.setIndexName("dwnhxs");
                break;
            case "单位污染物排放销售收入":preChartsDto.setIndexName("dwwrwpf");
                break;
            case "单位污染物排放营业收入":preChartsDto.setIndexName("dwwrwpf");
                break;
            case "研发经费投入强度":preChartsDto.setIndexName("yfjftr");
                break;
            case "全员劳动生产率":preChartsDto.setIndexName("qyldscl");
                break;
            case "单位用地销售收入":preChartsDto.setIndexName("dwydxs");
                break;
            case "单位用地营业收入":preChartsDto.setIndexName("dwydxs");
                break;
            case "收入利润率" : preChartsDto.setIndexName("srlrl");
                break;
            default:
                logger.info("无此选项!");
        }
        List<PreMuChartsVo> chartsVoList=  preMuBenefitEvaluationService.groupCompanyByIndustry(preChartsDto);

        BigDecimal sum = null;
        if(chartsVoList.size()!=0){
            sum = chartsVoList.stream().map(i -> new BigDecimal(i.getIndexNum())).reduce(BigDecimal::add).get();
            for (int i = 0; i < chartsVoList.size(); i++) {
                BigDecimal  indexNum=new BigDecimal(chartsVoList.get(i).getIndexNum());
                if(sum.compareTo(BigDecimal.valueOf(0))!=0){
                    BigDecimal result=indexNum.divide(sum,2,BigDecimal.ROUND_HALF_UP);
                    chartsVoList.get(i).setProportionLevel(String.valueOf(result.multiply(new BigDecimal(100))));
                }else{
                    chartsVoList.get(i).setProportionLevel("0");
                }
            }
        }
//            chartsVoList.sort((u4,u5)->{
//                Integer num1=Integer.valueOf(u4.getIndexNum()) ;
//                Integer num2=Integer.valueOf(u5.getIndexNum()) ;
//                return num2.compareTo(num1);
//            });
        chartsVoList.sort((u4,u5)->{
            BigDecimal num1 = new BigDecimal(u4.getIndexNum());
            BigDecimal num2 = new BigDecimal(u5.getIndexNum());
            return num2.compareTo(num1);
        });
        return success(chartsVoList);
    }


    @GetMapping(value = "/groupByArea")
    public AjaxResult groupByArea(PreMuChartsDto preChartsDto) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Set<String> roles= loginUser.getRoles();
        if(roles.contains("x_01_011")||roles.contains("along_yellow_river")){
            String county=  loginUser.getSysUser().getDistrict();
            preChartsDto.setCounty(county);
        }else if(roles.contains("s_01_011")){
            String city=  loginUser.getSysUser().getCity();
            preChartsDto.setCity(city);
        }else if(roles.contains("p_01_011")){
            if(StringUtils.isBlank(preChartsDto.getCity())){
                String province=  loginUser.getSysUser().getProvince();
                preChartsDto.setProvince(province);
            }
        }else if(roles.contains("admin")){
            if(StringUtils.isBlank(preChartsDto.getCity())){
                preChartsDto.setProvince("370000");
            }
        }
        String  indexName=preChartsDto.getIndexName();
        switch (indexName) {
            case "单位用地税收": preChartsDto.setIndexName("dwydss");
                break;
            case "单位能耗销售收入":preChartsDto.setIndexName("dwnhxs");
                break;
            case "单位能耗营业收入":preChartsDto.setIndexName("dwnhxs");
                break;
            case "单位污染物排放销售收入":preChartsDto.setIndexName("dwwrwpf");
                break;
            case "单位污染物排放营业收入":preChartsDto.setIndexName("dwwrwpf");
                break;
            case "研发经费投入强度":preChartsDto.setIndexName("yfjftr");
                break;
            case "全员劳动生产率":preChartsDto.setIndexName("qyldscl");
                break;
            case "单位用地销售收入":preChartsDto.setIndexName("dwydxs");
                break;
            case "单位用地营业收入":preChartsDto.setIndexName("dwydxs");
                break;
            case "收入利润率" : preChartsDto.setIndexName("srlrl");
                break;
            default:
                logger.info("无此选项!");
        }
        System.out.println(preChartsDto.getProvince()+"---city---"+preChartsDto.getCity()+"--county--:"+preChartsDto.getCounty());
        List<PreMuChartsVo> chartsVoList= preMuBenefitEvaluationService.groupByArea(preChartsDto);

        BigDecimal sum = null;
        if(chartsVoList.size()!=0){
            sum = chartsVoList.stream().map(i -> new BigDecimal(i.getIndexNum())).reduce(BigDecimal::add).get();
            for (int i = 0; i < chartsVoList.size(); i++) {
                BigDecimal  indexNum=new BigDecimal(chartsVoList.get(i).getIndexNum());
                if(sum.compareTo(BigDecimal.valueOf(0))!=0){
                    BigDecimal result=indexNum.divide(sum,2,BigDecimal.ROUND_HALF_UP);
                    chartsVoList.get(i).setProportionLevel(String.valueOf(result.multiply(new BigDecimal(100))));
                }else{
                    chartsVoList.get(i).setProportionLevel("0");
                }
            }
        }

        return success(chartsVoList);
    }

    @GetMapping(value = "/groupByAreaIndustry")
    public AjaxResult groupByAreaIndustry(PreMuChartsDto preChartsDto) {

        LoginUser loginUser = SecurityUtils.getLoginUser();
        Set<String> roles= loginUser.getRoles();
        if(roles.contains("x_01_011")||roles.contains("along_yellow_river")){
            String county=  loginUser.getSysUser().getDistrict();
            preChartsDto.setCounty(county);
        }else if(roles.contains("s_01_011")){
            String city=  loginUser.getSysUser().getCity();
            preChartsDto.setCity(city);
        }else if(roles.contains("p_01_011")){
            String province=  loginUser.getSysUser().getProvince();
            preChartsDto.setProvince(province);
        }
        String  indexName=preChartsDto.getIndexName();
        switch (indexName) {
            case "单位用地税收": preChartsDto.setIndexName("dwydss");
                break;
            case "单位能耗销售收入":preChartsDto.setIndexName("dwnhxs");
                break;
            case "单位能耗营业收入":preChartsDto.setIndexName("dwnhxs");
                break;
            case "单位污染物排放销售收入":preChartsDto.setIndexName("dwwrwpf");
                break;
            case "单位污染物排放营业收入":preChartsDto.setIndexName("dwwrwpf");
                break;
            case "研发经费投入强度":preChartsDto.setIndexName("yfjftr");
                break;
            case "全员劳动生产率":preChartsDto.setIndexName("qyldscl");
                break;
            case "单位用地销售收入":preChartsDto.setIndexName("dwydxs");
                break;
            case "单位用地营业收入":preChartsDto.setIndexName("dwydxs");
                break;
            case "收入利润率" : preChartsDto.setIndexName("srlrl");
                break;
            default:
                logger.info("无此选项!");
        }
        preChartsDto.setPageNum(0);
        preChartsDto.setPageSize(9);
        List<PreMuChartsVo> chartsVoList=  preMuBenefitEvaluationService.groupByAreaIndustry(preChartsDto);
        if(chartsVoList.size()>0){
            BigDecimal sum = null;
            BigDecimal bili = new BigDecimal("0");
            if(chartsVoList!=null){
                sum = chartsVoList.stream().map(i -> new BigDecimal(i.getIndexNum())).reduce(BigDecimal::add).get();
                for (int i = 0; i < chartsVoList.size(); i++) {
                    BigDecimal  indexNum=new BigDecimal(chartsVoList.get(i).getIndexNum());
                    if(sum.compareTo(BigDecimal.valueOf(0))!=0){
                        BigDecimal result=indexNum.divide(sum,2,BigDecimal.ROUND_HALF_UP);
                        chartsVoList.get(i).setProportionLevel(String.valueOf(result.multiply(new BigDecimal(100))));
                        bili=bili.add(result.multiply(new BigDecimal(100)));
                    }else{
                        chartsVoList.get(i).setProportionLevel("0");
                    }
                }
            }

           /* BigDecimal resultCount = new BigDecimal("100");
            PreMuChartsVo preMuChartsVo=new PreMuChartsVo();
            preMuChartsVo.setIndustryType("其他");
            BigDecimal result= resultCount.subtract(bili);
            preMuChartsVo.setProportionLevel(String.valueOf(result));
            chartsVoList.add(preMuChartsVo);*/
        }
        return success(PageUtils.paginateList(chartsVoList,preChartsDto.getPageNum()+1, preChartsDto.getPageSize()) );

    }
}
